import { FairyGUI } from 'csharp';
import FairyUtils from '../com/utils/FairyUtils';

export default class UIBaseWindow extends FairyGUI.GComponent{

    protected panel:FairyGUI.GComponent;
    protected btns:Array<FairyGUI.GButton> = [];
    protected btn_close:FairyGUI.GButton;

    constructor( pkgName:string , resName:string ){

        super();

        this.panel = FairyGUI.UIPackage.CreateObject( pkgName , resName ).asCom;
        // console.log( "------>pkgName: " + pkgName + " resName: "+resName );
        this.AddChild( this.panel );

        FairyUtils.setVar( this.panel , this , this.btns );   
        this.registerClass();
        this.initUI();     
    }

    public initUI():void{
        
    }

    protected registerClass(): void {

        // PanelRegister.registerClass( this._panelVo.pkgName );
    }

    public show( data:any ):void{

        let btn:FairyGUI.GButton;
        for( btn of this.btns ){
            if(!btn) continue;
            let callback1 = (context:FairyGUI.EventContext)=>{
                this.onBtnClick( context.sender as FairyGUI.GButton );
            }
            btn && btn.onClick.Clear();
            btn && btn.onClick.Add( callback1 );
            this[btn.name + "_EventCallBack"] = callback1;
        }
    }

    public hide():void{

        let btn:FairyGUI.GButton;
        for( btn of this.btns ){
            let callback1 = this[btn.name + "_EventCallBack"];
            btn && callback1 && btn.onClick.Remove( callback1 );
        }
        this.RemoveFromParent();
    }

    protected onBtnClick( btn:FairyGUI.GButton ):void{

        console.log( "------->onBtnClick: " + (btn?btn.name:btn) );
        if( btn == this.btn_close ){            
            this.hide();
        }
    }

    
}